package com.bootdo.wx.api;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberCouponService;
import com.bootdo.wx.service.MemberUserService;
import com.bootdo.wx.service.SalesCouponService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2019/6/13.
 */
@Controller
@RequestMapping("/api/memberCoupon")
public class ApiMemberCouponController {

    @Autowired
    private MemberCouponService memberCouponService;
    @Autowired
    private SalesCouponService salesCouponService;
    @Autowired
    private MemberUserService memberUserService;


    /*
  * 能领取的优惠券列表
  * */
    @ResponseBody
    @RequestMapping("/getNewCoupon")
    public R getNewCoupon(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ) {
                return R.error("参数错误");
            }
            String userId = paramMap.get("userId").toString();
            MemberUserDO user = memberUserService.get(userId);
            if (user == null) {
                return R.error("请重新登录");
            }
            List<Map<String,Object>> map = memberCouponService.getNewCoupon(paramMap);
            return R.ok().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }


    /*
   * 我的优惠券列表
   * type: 0：未使用：1：已使用：2已失效
   * */
    @ResponseBody
    @RequestMapping("/getMyCouponList")
    public R getMemberCouponList(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ||!paramMap.containsKey("status") || StringUtils.isBlank(paramMap.get("status").toString())
                    ) {
                return R.error("参数错误");
            }
            //Map<String, Object> map = memberCouponService.getMemberCouponList(paramMap);
            return memberCouponService.getMemberCouponList(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

/*
    *//*
     * 我的优惠券列表
     * type: 0：未使用：1：已使用：2已失效
     * *//*
    @ResponseBody
    @RequestMapping("/getMyCouponList")
    public R getMyCouponList(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ||!paramMap.containsKey("type") || StringUtils.isBlank(paramMap.get("type").toString())
            ) {
                return R.error("参数错误");
            }
            PageInfo<Map<String,Object>> map = memberCouponService.getMyCouponList(paramMap);
            return R.ok().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }*/

    /*
   * 查询我能用的的优惠券列表
   * */
    @ResponseBody
    @RequestMapping("/getMyCanCouponList")
    public R getMyCanCouponList(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ||!paramMap.containsKey("payment") || StringUtils.isBlank(paramMap.get("payment").toString())
                    ||!paramMap.containsKey("goodsId") || StringUtils.isBlank(paramMap.get("goodsId").toString())
                    ) {
                return R.error("参数错误");
            }
            paramMap.put("type","0");
            PageInfo<Map<String,Object>> map = memberCouponService.getMyCouponList(paramMap);
            return R.ok().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

    /*
  * 领取优惠券
  * */
    @ResponseBody
    @RequestMapping("/saveCoupon")
    public R saveCoupon(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())

                    ) {
                return R.error("参数错误");
            }
            String userId = paramMap.get("userId").toString();
            MemberUserDO user = memberUserService.get(userId);
            if (user == null) {
                return R.error("请重新登录");
            }
            List<Map<String,Object>> mapList = memberCouponService.getNewCoupon(paramMap);
            if (mapList.size() <= 0) {
                return R.error("该优惠券已领完");
            } else {
                return memberCouponService.saveCoupon(mapList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }









}
